package com.lebang.service.cences;

import com.lebang.domain.CencesDownloadMember;
import com.lebang.example.CencesDownloadMemberExample;
import com.lebang.mapper.CencesDownloadMemberMapper;
import com.lebang.mapper.DownloadMemberRecordMapper;
import com.lebang.mapper.LbbMemberMapper;
import com.lebang.model.MyConfig;
import com.lebang.model.Whether;
import com.lebang.model.cences.CencesMsg;
import com.lebang.model.cences.DownloadMemberFinishRecordCount;
import com.lebang.service.BaseService;
import com.lebang.service.mongodb.CencesMsgMongodb;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 每日必赚统计
 *
 * @Author hwq
 * @Date 2020-02-06
 */
@Service
@Slf4j
public class CencesDownloadMemberService extends BaseService<CencesDownloadMember, Integer, CencesDownloadMemberExample> {
    @Autowired
    private CencesDownloadMemberMapper baseMapper;
    @Autowired
    private LbbMemberMapper lbbMemberMapper;
    @Autowired
    private DownloadMemberRecordMapper recordMapper;
    @Autowired
    private CencesMsgMongodb cencesMsgMongodb;

    public void starDataInit(LocalDateTime end) {
        this.starDataInit(end, null);
    }

    public void starDataInit(LocalDateTime end, List<String> brand) {
        long starTime = System.currentTimeMillis();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDateTime star = end.minusDays(1);

        //统计日期
        int time = Integer.parseInt(end.toLocalDate().minusDays(1).format(df));

        List<CencesDownloadMember> cencesMasters = new ArrayList<>();
        if (brand == null) {
            //所有渠道
            brand = lbbMemberMapper.selectBrand();
        }

        List<DownloadMemberFinishRecordCount> downloadMemberFinishRecordCounts = recordMapper.countByCategory(star, end);
        downloadMemberFinishRecordCounts.forEach(s->{s.setAppStoreIsNewMember(s.getAppStore()+":"+s.getIsNewMember());});
        Map<String,List<DownloadMemberFinishRecordCount>> finishRecord = downloadMemberFinishRecordCounts.stream().collect(Collectors.groupingBy(DownloadMemberFinishRecordCount::getAppStoreIsNewMember));

        //完成每日必赚任务数
        AtomicInteger finishNum = new AtomicInteger(0);

        //单渠道
        brand.forEach(s -> {

            //单渠道新用户
            CencesDownloadMember singleBrandNewUser = new CencesDownloadMember(time, s, 1, 0, MyConfig.getEnv());
            //单渠道老用户
            CencesDownloadMember singleBrandOldUser = new CencesDownloadMember(time, s, 0, 0,MyConfig.getEnv());
            //单渠道全部用户
            CencesDownloadMember singleBrandAllUser = new CencesDownloadMember(time, s, 2, 0,MyConfig.getEnv());
            //完成一个任务
            finishNum.set(1);
            singleBrandNewUser.setOne(countFinish(finishRecord,finishNum.get(),s,Whether.YES.getDbIndex()));
            singleBrandOldUser.setOne(countFinish(finishRecord,finishNum.get(),s,Whether.NO.getDbIndex()));
            singleBrandAllUser.setOne(singleBrandNewUser.getOne()+singleBrandOldUser.getOne());
            //完成两个任务
            finishNum.set(2);
            singleBrandNewUser.setTwo(countFinish(finishRecord,finishNum.get(),s,Whether.YES.getDbIndex()));
            singleBrandOldUser.setTwo(countFinish(finishRecord,finishNum.get(),s,Whether.NO.getDbIndex()));
            singleBrandAllUser.setTwo(singleBrandNewUser.getTwo()+singleBrandOldUser.getTwo());
            //完成三个任务
            finishNum.set(3);
            singleBrandNewUser.setThree(countFinish(finishRecord,finishNum.get(),s,Whether.YES.getDbIndex()));
            singleBrandOldUser.setThree(countFinish(finishRecord,finishNum.get(),s,Whether.NO.getDbIndex()));
            singleBrandAllUser.setThree(singleBrandNewUser.getThree()+singleBrandOldUser.getThree());
            //完成四个任务
            finishNum.set(4);
            singleBrandNewUser.setFour(countFinish(finishRecord,finishNum.get(),s,Whether.YES.getDbIndex()));
            singleBrandOldUser.setFour(countFinish(finishRecord,finishNum.get(),s,Whether.NO.getDbIndex()));
            singleBrandAllUser.setFour(singleBrandNewUser.getFour()+singleBrandOldUser.getFour());
            //完成五个任务
            finishNum.set(5);
            singleBrandNewUser.setFive(countFinish(finishRecord,finishNum.get(),s,Whether.YES.getDbIndex()));
            singleBrandOldUser.setFive(countFinish(finishRecord,finishNum.get(),s,Whether.NO.getDbIndex()));
            singleBrandAllUser.setFive(singleBrandNewUser.getFive()+singleBrandOldUser.getFive());
            //完成六个任务或以上
            finishNum.set(6);
            singleBrandNewUser.setSix(countFinish(finishRecord,finishNum.get(),s,Whether.YES.getDbIndex()));
            singleBrandOldUser.setSix(countFinish(finishRecord,finishNum.get(),s,Whether.NO.getDbIndex()));
            singleBrandAllUser.setSix(singleBrandNewUser.getSix()+singleBrandOldUser.getSix());
            //有完成过任务的用户数
            singleBrandNewUser.setAllCount(singleBrandNewUser.getOne()+singleBrandNewUser.getTwo()+singleBrandNewUser.getThree()+singleBrandNewUser.getFour()+singleBrandNewUser.getFive()+singleBrandNewUser.getSix());
            singleBrandOldUser.setAllCount(singleBrandOldUser.getOne()+singleBrandOldUser.getTwo()+singleBrandOldUser.getThree()+singleBrandOldUser.getFour()+singleBrandOldUser.getFive()+singleBrandOldUser.getSix());
            singleBrandAllUser.setAllCount(singleBrandNewUser.getAllCount()+singleBrandOldUser.getAllCount());
            cencesMasters.add(singleBrandNewUser);
            cencesMasters.add(singleBrandOldUser);
            baseMapper.insertDownloadMember(singleBrandNewUser);
            baseMapper.insertDownloadMember(singleBrandOldUser);
            baseMapper.insertDownloadMember(singleBrandAllUser);
        });

        //全部渠道新用户
        CencesDownloadMember allBrandNewUser = new CencesDownloadMember(time, "all", 1, 1,MyConfig.getEnv());
        //全部渠道老用户
        CencesDownloadMember allBrandOldUser = new CencesDownloadMember(time, "all", 0, 1,MyConfig.getEnv());
        //全部渠道全部用户
        CencesDownloadMember allBrandAllUser = new CencesDownloadMember(time, "all", 2, 1,MyConfig.getEnv());

        cencesMasters.forEach(vo -> {
            sumBy(allBrandAllUser, vo);
            if (vo.getCategory().equals(1)) {
                sumBy(allBrandNewUser, vo);
            }
            if (vo.getCategory().equals(0)) {
                sumBy(allBrandOldUser, vo);
            }
        });
        baseMapper.insertDownloadMember(allBrandNewUser);
        baseMapper.insertDownloadMember(allBrandOldUser);
        baseMapper.insertDownloadMember(allBrandAllUser);

        cencesMsgMongodb.save(new CencesMsg("每日必赚完成人数统计",(System.currentTimeMillis() - starTime)));
    }



    public CencesDownloadMember sumData(Integer star, Integer end, String appStore, int category, int type) {
        return baseMapper.sumData(star, end, appStore, category, type);
    }

    private void sumBy(CencesDownloadMember item, CencesDownloadMember vo) {
        item.setOne(item.getOne() + vo.getOne());
        item.setTwo(item.getTwo() + vo.getTwo());
        item.setThree(item.getThree() + vo.getThree());
        item.setFour(item.getFour() + vo.getFour());
        item.setFive(item.getFive() + vo.getFive());
        item.setSix(item.getSix() + vo.getSix());
        item.setAllCount(item.getAllCount() + vo.getAllCount());
    }

    /**
     * 统计完成数人数
     * @param finishRecord 完成记录
     * @param finishNum 完成数
     * @param appStore 渠道
     * @param isNewMember 新老用户
     * @return 返回内容
     */
    private Integer countFinish(Map<String, List<DownloadMemberFinishRecordCount>> finishRecord, int finishNum, String appStore, Integer isNewMember) {
            AtomicInteger finish = new AtomicInteger(0);
            List<DownloadMemberFinishRecordCount> downloadMemberFinishRecordCounts = finishRecord.get(appStore+":"+isNewMember);
            if(null==downloadMemberFinishRecordCounts){
                return finish.get();
            }
            downloadMemberFinishRecordCounts.forEach(s->{
                if(finishNum==6){
                    if(s.getCount()>=finishNum){
                        finish.addAndGet(1);
                    }
                }else{
                    if(s.getCount()==finishNum){
                        finish.addAndGet(1);
                    }
                }

            });

        return finish.get();
    }

    public void deleteByTime(Integer time) {
        CencesDownloadMemberExample cencesDownloadMemberExample = new CencesDownloadMemberExample();
        cencesDownloadMemberExample.createCriteria().andTimeEqualTo(time).andAppIdEqualTo(MyConfig.getEnv());
        baseMapper.deleteByExample(cencesDownloadMemberExample);
    }
}
